﻿using System;
using System.Collections.Generic;
using System.Text;

// 版权所有：2014  All Rights Reserved.
// 文 件 名：ItemsBLL.cs
// 功能描述：
// 创建标识：m.sh.lin0328@163.com 2014/12/13 17:32:20
// 修改描述：
//----------------------------------------------------------------*/
namespace MslDiary.BLL
{
    public class A_ItemBLL : MSL.Tool.Data.DBAccessBase<Model.A_Item>
    {
        #region 构造

        public A_ItemBLL()
            : base("A_Item", "Id", false)
        {

        }
        #endregion

        public override int Add(Model.A_Item model)
        {
            object obj = MSL.Tool.Data.DbHelper.Instance.ExecScalar(MSL.Tool.Data.DbHelper.ConnectionString, "SELECT MAX(Id) FROM A_Item", new System.Collections.Hashtable());
            model.Id = Convert.ToInt32(obj.ToString()) + 1;
            return base.Add(model);
        }
        /// <summary>
        /// 获取插入 A_Itme 表中的语句 临时使用
        /// </summary>
        public void GetInsertItemSql()
        {
            StringBuilder sql = new StringBuilder();
            MSL.Tool.Data.DbHelper.ConnectionString = "Data Source=C:\\Temp\\MslDiary\\MslDiary\\bin\\x86\\Debug\\data\\cnmmmss_db.db;failifmissing=false;Pooling=True";
            System.Data.DataTable dt = MSL.Tool.Data.DbHelper.Instance.Query("SELECT * FROM A_Item ", new System.Collections.Hashtable());
            string tmp =
@"INSERT INTO A_Item(Id,FatherId,Name,EnName,Path,Sort,IsDelete,IsEnable,CreateDate,ModifyDate,IsAllowDelete)
VALUES({0},{1},'{2}','{3}','{4}',{5},'{6}','{7}','{8}','{9}','{10}');\r\n";
            if (dt != null || dt.Rows.Count > 0)
            {
                foreach (System.Data.DataRow dr in dt.Rows)
                {
                    sql.Append(
                        String.Format(
                                    tmp,
                                    dr["Id"].ToString(),
                                    dr["FatherId"].ToString(),
                                    dr["Name"] == null ? "" : dr["Name"].ToString(),
                                    dr["EnName"] == null ? "" : dr["EnName"].ToString(),
                                    dr["Path"] == null ? "" : dr["Path"].ToString(),
                                    dr["Sort"] == null ? "" : dr["Sort"].ToString(),
                                    dr["IsDelete"].ToString(),
                                    dr["IsEnable"].ToString(),
                                    DateTime.Now,
                                    DateTime.Now,
                                    dr["IsAllowDelete"].ToString()
                                    ));
                }
            }
        }
        public int DeleteById(int id, out string msg)
        {
            int num = base.Count(" FatherId=" + id);
            msg = String.Empty;
            if (num > 0)
            {
                msg = "存在子节点，不能删除！";
                return 0;
            }
            return base.Delete(" Id=" + id);
        }

        /// <summary>
        /// 天气图片 晴，\res\weather\weather_sun.png
        /// </summary>
        /// <returns></returns>
        public SortedList<string, string> GetWeatherImg()
        {
            SortedList<string, string> sortList = null;
            List<Model.A_Item> list = base.GetList(" IsDelete=0 AND IsEnable=1", " Name,Path", " Sort ASC");
            if (list != null && list.Count > 0)
            {
                sortList = new SortedList<string, string>();
                foreach (Model.A_Item mod in list)
                    sortList.Add(mod.Name, mod.Path);
            }
            return sortList;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetItemNameById(int id)
        {
            if (id == 0)
                return String.Empty;
            object obj = MSL.Tool.Data.DbHelper.Instance.ExecScalar("SELECT Name FROM A_Item WHERE Id= " + id, new System.Collections.Hashtable());
            if (obj != null)
                return obj.ToString();

            return String.Empty;
        }
    }
}
